package com.ruoyi.safe.controller.web;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.ListType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.safe.domain.*;
import com.ruoyi.safe.domain.dto.ProjectCheckRecordPhotonAlbumDTO;
import com.ruoyi.safe.domain.dto.UserSearchDTO;
import com.ruoyi.safe.domain.vo.BizSafeAttachBizInfo;
import com.ruoyi.safe.domain.vo.CommonUserVO;
import com.ruoyi.safe.service.*;
import com.ruoyi.system.domain.dto.CommonUserDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全相册Controller
 * 
 * @author ruoyi
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/attach/attach")
public class BizSafeAttachController extends BaseController
{
    @Autowired
    IBizSafeAttachService bizSafeAttachService;
    @Autowired
    ICommonProjectService commonProjectService;
    @Autowired
    ICommonUserService commonUserService;
    @Autowired
    IBizDutyRegionService bizDutyRegionService;
    @Autowired
    IListTypeService listTypeService;

    @Autowired
    IListConstructionService listConstructionService;
    @Autowired
    IListDeviseService listDeviseService;
    @Autowired
    IListMaintenanceService listMaintenanceService;
    @Autowired
    IListManageService listManageService;
    /**
     * 查询安全相册列表
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizSafeAttach bizSafeAttach)
    {
        startPage();
        List<BizSafeAttach> list = bizSafeAttachService.selectBizSafeAttachList(bizSafeAttach);
        return getDataTable(list);
    }

    /**
     * 导出安全相册列表
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:export')")
    @Log(title = "安全相册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizSafeAttach bizSafeAttach)
    {
        List<BizSafeAttach> list = bizSafeAttachService.selectBizSafeAttachList(bizSafeAttach);
        ExcelUtil<BizSafeAttach> util = new ExcelUtil<BizSafeAttach>(BizSafeAttach.class);
        util.exportExcel(response, list, "安全相册数据");
    }

    /**
     * 获取安全相册详细信息
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizSafeAttachService.selectBizSafeAttachById(id));
    }

    /**
     * 新增安全相册
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:add')")
    @Log(title = "安全相册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizSafeAttach bizSafeAttach)
    {
        return toAjax(bizSafeAttachService.insertBizSafeAttach(bizSafeAttach));
    }

    /**
     * 修改安全相册
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:edit')")
    @Log(title = "安全相册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSafeAttach bizSafeAttach)
    {
        return toAjax(bizSafeAttachService.updateBizSafeAttach(bizSafeAttach));
    }

    /**
     * 删除安全相册
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:remove')")
    @Log(title = "安全相册", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizSafeAttachService.deleteBizSafeAttachByIds(ids));
    }

    /**
     * 业务的安全相册
     */
    @PreAuthorize("@ss.hasPermi('attach:attach:photonAlbum')")
    @GetMapping("/photonAlbum")
    public TableDataInfo list(@Validated ProjectCheckRecordPhotonAlbumDTO projectCheckRecordPhotonAlbumDTO)
    {
        List<BizProjectCheckRecord> bizProjectCheckRecords = bizSafeAttachService.selectBizProjectCheckRecordList(projectCheckRecordPhotonAlbumDTO);
        List<Long> bizIds = bizProjectCheckRecords.stream().map(item->item.getId()).collect(Collectors.toList());
        if(bizIds.size() == 0){
            return getDataTable(new ArrayList<>());
        }else{
            BizSafeAttach bizSafeAttach = new BizSafeAttach();
            bizSafeAttach.setProjectId(projectCheckRecordPhotonAlbumDTO.getProjectId());
            bizSafeAttach.setBizIds(bizIds);
            bizSafeAttach.setStartTime(projectCheckRecordPhotonAlbumDTO.getStartTime());
            bizSafeAttach.setEndTime(projectCheckRecordPhotonAlbumDTO.getEndTime());
            bizSafeAttach.setCreateBy(projectCheckRecordPhotonAlbumDTO.getCreateBy());
            startPage();
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachList(bizSafeAttach);
            return getDataTable(bizSafeAttaches);
        }

    }
}
